package com.ydl.zhwc.mvpandrxjava.sample.token;

import com.ydl.zhwc.mvpandrxjava.bean.Now;
import com.ydl.zhwc.mvpandrxjava.mvp.RxPresenter;
import com.ydl.zhwc.mvpandrxjava.sample.RxUtils;

import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2017/12/22
 */

public class TokenPresenter extends RxPresenter<TokenContract.View> implements TokenContract.Presenter {

    private TokenContract.Model model;

    public TokenPresenter() {

        model = new TokenModel();
    }

    @Override
    public void loadNow() {
        Disposable disposable;

//        disposable = loadNow1();
        disposable = loadNow2();

        addSubscribe(disposable);
    }


    private Disposable loadNow1() {
        return model.getKey()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        model.getNow(s)
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<Now>() {
                                    @Override
                                    public void accept(Now now) throws Exception {
                                        view.updateUI(now);
                                    }
                                }, new Consumer<Throwable>() {
                                    @Override
                                    public void accept(Throwable throwable) throws Exception {
                                        view.showMsg(RxUtils.getErrorMsg(throwable));
                                    }
                                });

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showMsg(RxUtils.getErrorMsg(throwable));
                    }
                });
    }


    private Disposable loadNow2() {

        return model.getKey()
                .flatMap(new Function<String, ObservableSource<Now>>() {
                    @Override
                    public ObservableSource<Now> apply(String s) throws Exception {
                        return model.getNow(s);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Now>() {
                    @Override
                    public void accept(Now now) throws Exception {
                        view.updateUI(now);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showMsg(RxUtils.getErrorMsg(throwable));
                    }
                });

    }



}
